package com.lumi.max.jnicall.serialoperate;

import android.content.Context;
import android.hardware.usb.UsbManager;

import tw.com.prolific.driver.pl2303.PL2303Driver;

/**
 * Created by LME09 on 2016/3/18.
 */
public class PL2303Class extends SerialReadWrite {

    PL2303Driver mSerial;
    private static final String ACTION_USB_PERMISSION = "com.prolific.pl2303hxdsimpletest.USB_PERMISSION";

    private PL2303Driver.BaudRate mBaudrate = PL2303Driver.BaudRate.B115200;
    private PL2303Driver.DataBits mDataBits = PL2303Driver.DataBits.D8;
    private PL2303Driver.Parity mParity = PL2303Driver.Parity.NONE;
    private PL2303Driver.StopBits mStopBits = PL2303Driver.StopBits.S1;
    private PL2303Driver.FlowControl mFlowControl = PL2303Driver.FlowControl.OFF;
    int DevCount = -1;
    boolean uart_configured = false;

    @Override
    public void iniDevice(Context context) {
        if (mSerial == null) {
            mSerial = new PL2303Driver((UsbManager) context.getSystemService(Context.USB_SERVICE),
                    context, ACTION_USB_PERMISSION);
            DevCount = 1;
            if (!mSerial.PL2303USBFeatureSupported()) {
                mSerial = null;
                DevCount = 0;
            }
        }
    }

    @Override
    public boolean getUartConfiged() {
        return uart_configured;
    }

    @Override
    public int getDevCount() {
        return DevCount;
    }

    @Override
    public boolean isDeviceOpen() {
        if (mSerial == null){
            return false;
        }
        if (!mSerial.isConnected()) {
            return false;
//            if (!mSerial.enumerate()) {
//                return false;
//            } else {
//                return false;
//            }
        }//if isConnected
        return true;
    }

    @Override
    public void setConfig(int baudRate, byte dataBit, byte stopBit, byte parity, byte flowControl) {

        if (null == mSerial)
            return;
        if (mSerial.isConnected()) {
            switch (baudRate) {
                case 9600:
                    mBaudrate = PL2303Driver.BaudRate.B9600;
                    break;
                case 19200:
                    mBaudrate = PL2303Driver.BaudRate.B19200;
                    break;
                case 115200:
                    mBaudrate = PL2303Driver.BaudRate.B115200;
                    break;
                default:
                    mBaudrate = PL2303Driver.BaudRate.B9600;
                    break;
            }
        }

        if (!mSerial.InitByBaudRate(mBaudrate, 700)) {
            uart_configured = false;
            if (!mSerial.PL2303Device_IsHasPermission()) {
            }

            if (mSerial.PL2303Device_IsHasPermission() && (!mSerial.PL2303Device_IsSupportChip())) {
            }
        } else {
            uart_configured = true;
        }

    }

    @Override
    public void writeData(byte[] OutData) {
        int res = mSerial.write(OutData);
    }


//    @Override
//    public int readTask(char[] readData) {
//        if (mSerial == null){
//            return 0;
//        }
//        int len;
//        byte[] readByteData = new byte[20];
//        len = mSerial.read(readByteData);
//        if (len > 0) {
//            for (int j = 0; j < len; j++) {
//                readData[j] = (char) readByteData[j];
//            }
//        }
//        return len;
//    }

    @Override
    public String readTask() {
        if (mSerial == null){
            return null;
        }
        int len;
        byte[] buf = new byte[2000];
        len = mSerial.read(buf);
        if (len > 0) {
//            byte[] data = new byte[len];
//            System.arraycopy(buf,0,data,0,data.length);
//            return data;
            return toHex(buf, 0, len);
        }
        return null;
    }


    @Override
    public void enableRead(boolean flag) {

    }

    @Override
    public void connectFunction(Context context) {
        // if (!mSerial.InitByBaudRate(mBaud

        if (null == mSerial)
            return;
        if (!mSerial.isConnected()) {
            if (!mSerial.enumerate()) {
//                Toast.makeText(this, "no more devices found", Toast.LENGTH_SHORT).show();
            } else {
//                Log.d(TAG, "enumerate succeeded!");
            }
        }//if isConnected

//        if (!mSerial.InitByBaudRate(mBaudrate, 700)) {
//            uart_configured = false;
//            if (!mSerial.PL2303Device_IsHasPermission()) {
//            }
//
//            if (mSerial.PL2303Device_IsHasPermission() && (!mSerial.PL2303Device_IsSupportChip())) {
//            }
//        } else {
//            uart_configured = true;
//        }
    }

    @Override
    public void disconnectFunction() {
        if (mSerial != null) {
            mSerial.end();
            mSerial = null;
        }
    }
}
